import annotation.Book;
import annotation.BookStore;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Properties;

public class TestMain {

    @Test
    public void test() throws FileNotFoundException {
        try {
            Class testMain = Class.forName("TestMain");
            Class testMainClass = TestMain.class;
            Class aClass = new TestMain().getClass();
            System.out.println(testMain);
            System.out.println(testMainClass);
            System.out.println(aClass);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test2() throws FileNotFoundException {
        try {
            Class<?> aClass = Class.forName("entity.User");
            Field[] fields = aClass.getFields();
            for (Field field : fields) {
                System.out.println(field.getName());
            }
//            Field username = aClass.getField("username");
//            System.out.println(username.getName());

            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                System.out.println(field.getName());
            }
            Field username1 = aClass.getDeclaredField("username");
            System.out.println(username1.getName());


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test3() throws FileNotFoundException {
        try {
            // 加载配置文件
            InputStream inputStream = TestMain.class.getClassLoader().getResourceAsStream("pro.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            // className   methodName
            String className = properties.getProperty("className");
            String methodName = properties.getProperty("methodName");

            Class<?> aClass = Class.forName(className);
            Method method = aClass.getMethod(methodName);
            Object o = aClass.newInstance();
            method.invoke(o);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test4() throws FileNotFoundException {
        // 获取类上的注解
        try {
            Class<?> aClass = Class.forName("annotation.BookStore");
            if (aClass.isAnnotationPresent(Book.class)) {
                // 有注解
                Book book = aClass.getAnnotation(Book.class);
                System.out.println(book.value());
                System.out.println(book.price());
                System.out.println(book.authors());
                System.out.println(Arrays.toString(book.authors()));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void test5() throws FileNotFoundException {
        try {
            Class<BookStore> bookStoreClass = BookStore.class;
            Method buyBook = bookStoreClass.getDeclaredMethod("buyBook");
            if (buyBook.isAnnotationPresent(Book.class)) {

                Book book = buyBook.getAnnotation(Book.class);
                System.out.println(book.value());
                System.out.println(book.price());
                System.out.println(Arrays.toString(book.authors()));
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
